﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.InfoConfig.Controller;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig
{
    public partial class FrmTemplateManager :FrmMDIChildFormTemplate
    {
        private PaperTemplateController _controller = new PaperTemplateController();
        private PaperFaceController _paperFaceController = new PaperFaceController();
        private PaperTypeController _paperTypeController = new PaperTypeController();
        private PrinterController _printerController = new PrinterController();
        PaperTemplateEntity _paperTemplateEntity = null;
        static FrmTemplateManager instance = null;
        private FrmTemplateManager()
        {
            InitializeComponent();
        }

        public static FrmTemplateManager CreateInstance
        {
            get
            {
                if (instance == null)
                    instance = new FrmTemplateManager();
                return instance;
            }
        }

        private void FrmTemplateManager_Load(object sender, EventArgs e)
        {
            _controller.Grid = gridControlPaperTemplate;
            _controller.View = gridViewPaperTemplate;
            _controller.GetDate();

            _controller.ComoBox = cmbPaperType;
            _controller.LinkcmbPaperType();

            _controller.ComoBox = cmbPaperFace;
            _controller.LinkcmbPaperFace();

            _controller.ComoBox = cmbPrinter;
            _controller.LinkcmbPrinter();
        }
        private void gridViewPaperTemplate_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _paperTemplateEntity = (PaperTemplateEntity)gridViewPaperTemplate.GetFocusedRow();
            if (_paperTemplateEntity != null)
            {
                txtHeight.Text = _paperTemplateEntity.BoardHeight.ToString();
                txtlength.Text = _paperTemplateEntity.BoardLength.ToString();
                txtPaperTemplateName.Text = _paperTemplateEntity.TemplateName;
                cmbPaperFace.Text = _paperTemplateEntity.PaperFaceName;
                cmbPaperType.Text = _paperTemplateEntity.PaperTypeName;
                cmbPrinter.Text = _paperTemplateEntity.PName;
            }
        }

        // 添加纸模版
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtPaperTemplateName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_paperTemplate obj = new tb_paperTemplate();

                obj.TemplateName = txtPaperTemplateName.Text;
                obj.boardHeight =Convert.ToDouble( txtHeight.Text);
                obj.boardLength =Convert.ToDouble( txtlength.Text);
                obj.haveBarCode = true;
                paperFace face = _paperFaceController.GetPaperFaceObj(cmbPaperFace.Text);
                paperType type = _paperTypeController.GetPaperTypeObj(cmbPaperType.Text);
                printer printer = _printerController.GetPrinterObj(cmbPrinter.Text);
                obj.paperFace = face;
                obj.paperType = type;
                obj.printer = printer;
                _controller.Add(obj);

                //refresh datagrid
                _controller.Grid = gridControlPaperTemplate;
                _controller.View = gridViewPaperTemplate;
                _controller.GetDate();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        // 更新纸模版
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtPaperTemplateName.Focus();
                return;
            }
            if (CheckData())
            {
                if (_paperTemplateEntity != null)
                {
                    _paperTemplateEntity.BoardHeight = Convert.ToDouble(txtHeight.Text);
                    _paperTemplateEntity.BoardLength = Convert.ToDouble(txtlength.Text);
                    _paperTemplateEntity.PaperFaceName = cmbPaperFace.Text;
                    _paperTemplateEntity.PaperTypeName = cmbPaperType.Text;
                    _paperTemplateEntity.PName = cmbPrinter.Text;
                    _paperTemplateEntity.TemplateName = txtPaperTemplateName.Text;
                    _controller.Update(_paperTemplateEntity);

                    //link datagrid
                    _controller.GetDate();

                    PrepareUpdate(false);

                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("请选择要更新的模版！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }
        // 删除纸模版
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (_paperTemplateEntity != null)
            {
                if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    _controller.Delete(_paperTemplateEntity);

                    _controller.GetDate();
                }
            }
            else 
            {
                MessageBox.Show("请选择要删除的模板", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        // 取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtPaperTemplateName.Enabled = false ;
            txtHeight.Enabled = false;
            txtlength.Enabled = false;
            cmbPaperFace.Enabled = false;
            cmbPaperType.Enabled = false;
            cmbPrinter.Enabled = false;


            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnUpdate.Enabled = true;
            btnAdd.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtPaperTemplateName.Text == string.Empty)
            {
                txtPaperTemplateName.Focus();
                MessageBox.Show("纸模版名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtHeight.Text == string.Empty)
            {
                txtHeight.Focus();
                MessageBox.Show("板高不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtlength.Text == string.Empty)
            {
                txtlength.Focus();
                MessageBox.Show("板长不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbPaperFace.Text  == string.Empty)
            {
                cmbPaperFace.Focus();
                MessageBox.Show("纸面不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbPaperType.Text == string.Empty)
            {
                cmbPaperType.Focus();
                MessageBox.Show("纸型不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbPrinter.Text == string.Empty)
            {
                cmbPrinter.Focus();
                MessageBox.Show("打印机不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtPaperTemplateName.Enabled = bl;
            txtHeight.Enabled = bl;
            txtlength.Enabled = bl;
            cmbPaperFace.Enabled = bl;
            cmbPaperType.Enabled = bl;
            cmbPrinter.Enabled = bl;

            txtPaperTemplateName.Text = string.Empty;
            txtHeight.Text = string.Empty;
            txtlength.Text = string.Empty;

            if (bl)
            {

                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtPaperTemplateName.Enabled = bl;
            txtHeight.Enabled = bl;
            txtlength.Enabled = bl;
            cmbPaperFace.Enabled = bl;
            cmbPaperType.Enabled = bl;
            cmbPrinter.Enabled = bl;

            if (bl)
            {
                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmTemplateManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void txtHeight_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                Convert.ToDouble(txtHeight.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtHeight.Text = string.Empty;
            } 
        }

        private void txtlength_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                Convert.ToDouble(txtlength.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtlength.Text = string.Empty;
            } 
        }
    }
}
